package cn.iocoder.yudao.module.resource.controller.admin.advertisement;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.resource.controller.admin.advertisement.vo.ResourceAdvertisementPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.advertisement.vo.ResourceAdvertisementRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.advertisement.vo.ResourceAdvertisementSaveReqVO;
import cn.iocoder.yudao.module.resource.convert.ResourceAdvertisementConvert;
import cn.iocoder.yudao.module.resource.dal.dataobject.advertisement.ResourceAdvertisementDO;
import cn.iocoder.yudao.module.resource.service.advertisement.ResourceAdvertisementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 广告")
@RestController
@RequestMapping("/resource/advertisement")
@Validated
public class ResourceAdvertisementController {

    @Resource
    private ResourceAdvertisementService advertisementService;

    @PostMapping("/create")
    @Operation(summary = "创建广告")
    @PreAuthorize("@ss.hasPermission('resource:advertisement:create')")
    public CommonResult<Long> createAdvertisement(@Valid @RequestBody ResourceAdvertisementSaveReqVO createReqVO) {
        return success(advertisementService.createAdvertisement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新广告")
    @PreAuthorize("@ss.hasPermission('resource:advertisement:update')")
    public CommonResult<Boolean> updateAdvertisement(@Valid @RequestBody ResourceAdvertisementSaveReqVO updateReqVO) {
        advertisementService.updateAdvertisement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除广告")
    @Parameter(name = "id", description = "广告ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:advertisement:delete')")
    public CommonResult<Boolean> deleteAdvertisement(@RequestParam("id") Long id) {
        advertisementService.deleteAdvertisement(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除广告")
    @Parameter(name = "ids", description = "广告ID列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('resource:advertisement:delete')")
    public CommonResult<Boolean> deleteAdvertisementList(@RequestParam("ids") List<Long> ids) {
        advertisementService.deleteAdvertisementList(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得广告")
    @Parameter(name = "id", description = "广告ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:advertisement:query')")
    public CommonResult<ResourceAdvertisementRespVO> getAdvertisement(@RequestParam("id") Long id) {
        ResourceAdvertisementDO advertisement = advertisementService.getAdvertisement(id);
        return success(BeanUtils.toBean(advertisement, ResourceAdvertisementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得广告分页")
    @PreAuthorize("@ss.hasPermission('resource:advertisement:query')")
    public CommonResult<PageResult<ResourceAdvertisementRespVO>> getAdvertisementPage(@Valid ResourceAdvertisementPageReqVO pageReqVO) {
        PageResult<ResourceAdvertisementDO> pageResult = advertisementService.getAdvertisementPage(pageReqVO);
        return success(ResourceAdvertisementConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出广告 Excel")
    @PreAuthorize("@ss.hasPermission('resource:advertisement:export')")
    public void exportAdvertisementExcel(@Valid ResourceAdvertisementPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceAdvertisementDO> list = advertisementService.getAdvertisementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "广告.xls", "数据", ResourceAdvertisementRespVO.class,
                BeanUtils.toBean(list, ResourceAdvertisementRespVO.class));
    }

}
